package com.academic.system.service.impl;

import com.academic.system.model.User;
import com.academic.system.model.UserRole;
import com.academic.system.repository.UserRepository;
import com.academic.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public User findById(Integer id) {
        return userRepository.findById(id);
    }

    @Override
    public User findByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    @Override
    public User createUser(User user) {
        userRepository.insert(user);
        return user;
    }

    @Override
    public User updateUser(User user) {
        userRepository.update(user);
        return user;
    }

    @Override
    public void deleteUser(Integer id) {
        userRepository.deleteById(id);
    }

    @Override
    public void updateLastLoginTime(Integer userId) {
        User user = userRepository.findById(userId);
        if (user != null) {
            user.setLastLogin(LocalDateTime.now());
            userRepository.update(user);
        }
    }

    @Override
    public List<User> findStudentsByTeacherId(Integer teacherId) {
        return userRepository.findByCreatorIdAndRole(teacherId, UserRole.STUDENT);
    }

    @Override
    public List<User> findAllTeachers() {
        return userRepository.findByRole(UserRole.TEACHER);
    }
} 